<template>
  <div class="cuisineBox">
    <el-tabs
      type="border-card"
      class="cuisineTabs"
    >
      <el-button
        type="primary"
        round
        class="upload"
        @click="uploadDish"
      >上传菜品<i class="el-icon-upload el-icon--right"></i>
      </el-button>
      <div
        class="buyCar"
        @click="openBuyCar"
      >
        <el-badge
          :value="this.$store.getters.doneCount"
          :max="99"
          class="badge"
        >
          <i class="el-icon-shopping-cart-full"></i>
        </el-badge>
      </div>
      <el-drawer
        title="我的购物车"
        :visible.sync="drawer"
        :with-header="true"
      >
        <el-row :gutter="20">
          <el-col :span="6">
            <div class="grid-content bg-purple">菜品名称</div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">价格(元)</div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">数量</div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">总计(元)</div>
          </el-col>
        </el-row>
        <el-row
          :gutter="20"
          style="font-size:16px"
          v-for="item in buyCarData"
          :key="item.dishId"
        >
          <el-col :span="6">
            <div class="grid-content bg-purple">{{item.dishName}}</div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">{{item.presentPrice}}</div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">{{item.buyNum}}</div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">{{item.buyNum*item.presentPrice}}</div>
          </el-col>
        </el-row>
        <div>
          <span>购物车总计：{{sumPrice}}元</span>
        </div>
      </el-drawer>
      <el-tab-pane>
        <span slot="label">
          <i class="iconfont  iconzhongcan"></i>
          主菜
        </span>
        <chinese-foods
          :mainDish="mainDishIndex"
          ref="foodChild1"
        />
      </el-tab-pane>
      <el-tab-pane>
        <span slot="label">
          <i class="iconfont  icon_xican"></i>
          汤和凉菜
        </span>
        <chinese-foods
          :mainDish="soupDishIndex"
          ref="foodChild2"
        />
      </el-tab-pane>
      <el-tab-pane>
        <span slot="label">
          <i class="iconfont  iconyinliao"></i>
          饮料
        </span>
        <chinese-foods
          :mainDish="drinkDishIndex"
          ref="foodChild3"
        />
      </el-tab-pane>
      <el-tab-pane>
        <span slot="label">
          <i class="iconfont  icontiandianmianbaodangaoshuiguotiandian"></i>
          饭后甜点
        </span>
        <chinese-foods
          :mainDish="dessertsDishIndex"
          ref="foodChild4"
        />
      </el-tab-pane>
    </el-tabs>
    <!-- 弹出框modal -->
    <el-dialog
      title="上传菜品"
      center
      width="50%"
      :visible.sync="dishVisible"
      @close="cancel"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="菜品名"
          prop="dishName"
        >
          <el-input
            v-model="ruleForm.dishName"
            style="width:300px"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="原价"
          prop="originalPrice"
        >
          <el-input
            v-model="ruleForm.originalPrice"
            oninput="value=value.replace(/[^\d]/g,'')"
            style="width:300px"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="现价"
          prop="presentPrice"
        >
          <el-input
            v-model="ruleForm.presentPrice"
            oninput="value=value.replace(/[^\d]/g,'')"
            style="width:300px"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="菜品类型"
          prop="dishType"
        >
          <el-select
            v-model="ruleForm.dishType"
            placeholder="请选择菜品类型"
          >
            <el-option
              label="主菜"
              value="1"
            ></el-option>
            <el-option
              label="汤和凉菜"
              value="2"
            ></el-option>
            <el-option
              label="饮料"
              value="3"
            ></el-option>
            <el-option
              label="甜点"
              value="4"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="是否上架"
          prop="isVaild"
        >
          <el-radio-group v-model="ruleForm.isVaild">
            <el-radio label="上架"></el-radio>
            <el-radio label="下架"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="菜品描述"
          prop="dishIntroduce"
        >
          <el-input
            type="textarea"
            :rows="6"
            v-model="ruleForm.dishIntroduce"
            show-word-limit="true"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-upload
        :on-success="hand"
        class="upload-demo"
        drag
        :before-upload="handleUpload"
        action="https://jsonplaceholder.typicode.com/posts/"
        multiple
        ref="my-upload"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将菜品图片拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button
          type="primary"
          @click="sure"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import chineseFoods from './chineseFoods.vue'
import interfaceList from '../../utils/interface'
import axios from 'axios'
export default {
  name:'dishIndex',
  data(){
    return{
      mainDishIndex:1,
      soupDishIndex:2,
      drinkDishIndex:3,
      dessertsDishIndex:4,
      dishVisible:false,
      drawer:false,
      fileUrl:'',
      buyCarData:[],
      ruleForm: {
        dishName: '',
        originalPrice: '',
        isVaild: '上架',
        presentPrice: '',
        dishType:'',
        dishIntroduce:'',
      },
      rules: {
        dishName: [
          { required: true, message: '请输入菜品名称', trigger: 'blur' }
        ],
        originalPrice: [
          { required: true, message: '请输入菜品原价格', trigger: 'blur' }
        ],
        isVaild: [
            { required: true, message: '请选择菜品是否上架', trigger: 'change' }
        ],
        presentPrice: [
          { required: true, message: '请输入菜品现价格', trigger: 'blur' }
        ],
        dishType: [
          { required: true, message: '请选择菜品类型', trigger: 'change' },
        ],
        dishIntroduce: [
          { required: true, message: '请输入菜品描述', trigger: 'blur' },
        ],
      },
    }
  },
  methods:{
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.clearFiles()
    },
    cancel(){
      this.dishVisible = false
      this.$refs.ruleForm.resetFields()
      this.clearFiles()
    },
    sure(){
      // console.log(this.fileUrl)
      this.ruleForm.isVaild = this.ruleForm.isVaild === '上架' ? "1" : "0"
      let fd = new FormData()
      fd.append('dishName',this.ruleForm.dishName);
      fd.append('imgUrl',this.fileUrl);
      fd.append('originalPrice',this.ruleForm.originalPrice);
      fd.append('presentPrice',this.ruleForm.presentPrice);
      fd.append('isVaild',this.ruleForm.isVaild);
      fd.append('dishType',this.ruleForm.dishType);
      fd.append('dishIntroduce',this.ruleForm.dishIntroduce);
      // console.log(this.ruleForm)
      // console.log(fd)
      axios({
          method:"post",
          url:"/api/dish/upload",
          // headers:{
          //     'Content-type': 'multipart/form-data'
          // },
          data:fd
      }).then((res)=>{
          console.log(res.data)
          if(res.data.code === 200){
            this.$refs[`foodChild${this.ruleForm.dishType}`].getList(this.ruleForm.dishType)
            // this.$refs.foodChild.dishAddFlag = this.ruleForm.dishType
            // this.getList(this.mainDish)
            this.dishVisible = false
            this.$refs.ruleForm.resetFields()
          }
      })
    },
    handleUpload(file){
      // console.log(file)
      this.fileUrl = file
      return true
    },
    hand(response, file, fileList){
      console.log(response, file, fileList)
    },
    clearFiles () {
        this.$refs['my-upload'].clearFiles();
    },
    openBuyCar(){
      this.drawer=true
      this.buyCarData = this.$store.getters.doneBuyCar
      console.log(this.$store.getters.doneBuyCar)
    },
    uploadDish () {
      interfaceList.getUser().then(res => {
        console.log(res)
        if(res.code === 200){
          for (let i = 0; i < res.userName.length; i++) {
            if (res.userName[i].username === sessionStorage.getItem('loginName')) {
              this.dishVisible = true
              break
            }
            if (i === res.userName.length - 1) {
                alert('很抱歉您没有权限操作！')
              }
          }
        }
      })
    }
  },
  created(){},
  components: { chineseFoods },
  computed:{
    sumPrice(){
      if(this.buyCarData.lenth === 0){
        return 0
      }else{
        let sum = 0
        this.buyCarData.forEach(element => {
          sum += element.presentPrice*element.buyNum
        })
        return sum
      }
    }
  }

}
</script>

<style lang="scss" scoped>
.demo-ruleForm {
  position: relative;
  top: 0;
  left: 100px;
}
.upload-demo {
  margin-left: 150px;
}
.upload {
  margin: 0 0 10px 0;
}
.buyCar {
  float: right;
  margin-right: 100px;
  cursor: pointer;
  .badge {
    margin-top: 10px;
    margin-right: 40px;
    .el-icon-shopping-cart-full {
      font-size: 40px;
      color: #ff0018;
    }
  }
}
.cuisineTabs {
  border: 1px solid #d4dce6;
}

.el-row {
  margin-bottom: 20px;
  text-align: center;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  // background: #99a9bf;
}
.bg-purple {
  // background: #d3dce6;
}
.bg-purple-light {
  // background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  // background-color: #f9fafc;
}
</style>
<style>
.el-badge__content {
  background-color: #ff0018;
}
.el-tabs__nav {
  width: 100% !important;
}
.el-tabs__item {
  text-align: center;
  width: 25%;
  border-right: 1px solid gray !important;
  border-left: 1px solid gray !important;
}
.el-tabs__content {
  height: 100%;
  /* overflow:scroll; */
}
</style>